/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLLERS;

import BEANS.ComboItem;
import UTIL.DatabaseConnector;
import UTIL.InputValidator;
import UTIL.TableManager;
import datechooser.beans.DateChooserCombo;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Belthazod
 */
public class ReturnsController {
    private DatabaseConnector dbConnector = DatabaseConnector.getInstance();
    private TableManager defectiveItemsTableManager;
    private TableManager returnsTableManager;
    private TableManager returnedTableManager;
    private TableManager replacedTableManager;
    private TableManager returnsHistoryTableManager;
    private JComboBox<ComboItem> supplierComboBox; 
    private JComboBox<ComboItem> returnedSupplierComboBox; 
    private DateChooserCombo dateReturnedDateChooser;
    private DateChooserCombo dateReplacedDateChooser;
    private JButton addButton;
    private JButton addAllButton;
    private JLabel inputQuantityTotal;
    private JLabel inputQuantityNameLabel;
    private JLabel supplierName;
    private JLabel totalQuantityLabel;
    private JLabel replacedSupplierText;
    private JTextField inputQuantityInput;
    private JTextField replacementQuantityInput;
    private JTextArea remarksInput;
    private JTextArea replacedRemarksInput;
    private JDialog inputQuantityDialog;
    private JDialog replacementInputQuantityDialog;
    private JLabel info;
    private static ArrayList<String> returnsIDs;
    private static ArrayList<String> returnedIDs;
    DateFormat dfto = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdf = new SimpleDateFormat("MMMMM d, yyyy");
    private DateChooserCombo dateTo;
    private DateChooserCombo dateFrom;
    private JTextField editQuantityInput;
    private JButton add;
    
    public ReturnsController(JTable defectiveItemsTable, JTable returnsTable, 
            JComboBox<ComboItem> supplierComboBox, 
            DateChooserCombo dateReturnedDateChooser, 
            JButton addButton, JTextArea remarksInput, JDialog inputQuantityDialog,
            JLabel inputQuantityNameLabel, JLabel inputQuantityTotal,
            JTextField inputQuantityInput, JLabel supplierName , JLabel info,
            JTable returnedTable, JTable replacedTable, JDialog replacementInputQuantityDialog,
            JComboBox<ComboItem> returnedSupplierFilter, JLabel totalQuantityText,
            JTextField replacementQuantityInput, JLabel replacedSupplierText,
            DateChooserCombo dateReplacedDateChooser, JTextArea replacedRemarksInput,
            DateChooserCombo dateTo, DateChooserCombo dateFrom, JTable returnsHistoryTable, JTextField editQuantityInput,
            JButton add){
        defectiveItemsTableManager = new TableManager(defectiveItemsTable);
        returnsTableManager = new TableManager(returnsTable);
        returnsHistoryTableManager = new TableManager(returnsHistoryTable);
        this.add = add;
        this.editQuantityInput = editQuantityInput;
        this.dateTo = dateTo;
        this.dateFrom = dateFrom;
        this.supplierComboBox = supplierComboBox;
        this.dateReturnedDateChooser = dateReturnedDateChooser;
        this.addButton = addButton;
        this.remarksInput = remarksInput;
        this.inputQuantityDialog = inputQuantityDialog;
        this.inputQuantityInput = inputQuantityInput;
        this.inputQuantityNameLabel = inputQuantityNameLabel;
        this.inputQuantityTotal = inputQuantityTotal;
        this.supplierName = supplierName;
        this.info = info;
        this.replacementInputQuantityDialog = replacementInputQuantityDialog;
        this.returnedSupplierComboBox = returnedSupplierFilter;
        this.totalQuantityLabel = totalQuantityText;
        this.replacementQuantityInput = replacementQuantityInput;
        this.replacedSupplierText = replacedSupplierText;
        this.dateReplacedDateChooser = dateReplacedDateChooser;
        this.replacedRemarksInput = replacedRemarksInput;
        returnedTableManager = new TableManager(returnedTable);
        replacedTableManager = new TableManager(replacedTable);
        returnsIDs = new ArrayList<String>();
        returnedIDs = new ArrayList<String>();
        
        returnedTableManager.hideColumn(0);
        replacedTableManager.hideColumn(0);
        replacementInputQuantityDialog.setLocationRelativeTo(null);
        returnsTableManager.hideColumn(0);
        defectiveItemsTableManager.hideColumn(0);
    }
    
    public void updateReturnsTable(){
        try{
            ResultSet rs = dbConnector.query("SELECT return_id, rep_date, name, serial_number, return_quantity, replacements.remarks, R1.supplier_id, supplier_name, return_date FROM product JOIN returns R1 USING(product_id) JOIN replacements USING(replacement_id) JOIN supplier ON R1.supplier_id = supplier.supplier_id ORDER BY 2 DESC");
            defectiveItemsTableManager.clearTableContents();
            while(rs.next()){
                String returnID = rs.getString(1);
                String repDate = rs.getString(2);
                String productName = rs.getString(3);
                String serialNumber = rs.getString(4);
                String returnQuantity = rs.getString(5);
                String remarks = rs.getString(6);
                String supplier = rs.getString(7);
                String supplierName = rs.getString(8);
                Date date = dfto.parse(repDate);
                String replacementDate = sdf.format(date);
                String returnDate = rs.getString(9);
                
                Object supplierComboBoxitem = supplierComboBox.getSelectedItem();
                ComboItem ci = ((ComboItem)supplierComboBoxitem);
                String supplierID = ci.getValue();
                
                if(supplierComboBox.getSelectedItem().toString().equals("All")||supplierID.equals(supplier)){
                    if(returnsIDs.contains(returnID)){
                        for(int i = 0; i<returnsTableManager.getRowCount(); i++){
                            if(returnID.equals(returnsTableManager.getIDFromTable(i)) && Integer.parseInt(returnQuantity) > Integer.parseInt(returnsTableManager.getValueAt(i, 3))){
                                int partialQuantity = Integer.parseInt(returnQuantity) - Integer.parseInt(returnsTableManager.getValueAt(i, 3));
                                //System.out.print("Partial:" + partialQuantity);
                                
                                defectiveItemsTableManager.addRowContent(new String[]{returnID, replacementDate, productName, supplierName, serialNumber, Integer.toString(partialQuantity), remarks});
                            }
                        }

                    }else{
                        try{
                            if(returnDate == null){
                                defectiveItemsTableManager.addRowContent(new String[]{returnID, replacementDate, productName, supplierName, serialNumber, returnQuantity, remarks});
                            }
                        }catch(NullPointerException npe){
                            defectiveItemsTableManager.addRowContent(new String[]{returnID, replacementDate, productName, supplierName, serialNumber, returnQuantity, remarks});
                        }
                    }
                }
            }
        }catch(SQLException sqlE){
            sqlE.printStackTrace();
        }catch(NullPointerException npe){
        
        }catch(ParseException pe){
            pe.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public void addToReturnsTable(){
        try{
            String returnID = defectiveItemsTableManager.getIDFromTable(defectiveItemsTableManager.getSelectedRow());
            String productName = defectiveItemsTableManager.getValueAt(defectiveItemsTableManager.getSelectedRow(), 2);
            String serial;
            try{
                serial = defectiveItemsTableManager.getValueAt(defectiveItemsTableManager.getSelectedRow(), 4);
            }catch(NullPointerException npe){
                serial = "";    
            }
            String quantity = defectiveItemsTableManager.getValueAt(defectiveItemsTableManager.getSelectedRow(), 5);
            String remarks = defectiveItemsTableManager.getValueAt(defectiveItemsTableManager.getSelectedRow(), 6);
            
            if(returnsIDs.contains(returnID)){
                JOptionPane.showMessageDialog(null, "The product is already in the list.", "Item already in list", JOptionPane.ERROR_MESSAGE);
            }else{
                if(Integer.parseInt(quantity) > 1){
                    inputQuantityTotal.setText(quantity);
                    inputQuantityNameLabel.setText(productName);
                    inputQuantityDialog.setVisible(true);
                }else{
                    returnsIDs.add(returnID);
                    returnsTableManager.addRowContent(new String[]{returnID, productName, serial, quantity, remarks, quantity});
                    updateReturnsTable();
                }
            }
        }catch(ArrayIndexOutOfBoundsException aioob){
            JOptionPane.showMessageDialog(null, "Please select and item from the table.", "No item selected", JOptionPane.ERROR_MESSAGE);
        }catch(IndexOutOfBoundsException ioobe){
            JOptionPane.showMessageDialog(null, "Please select and item from the table.", "No item selected", JOptionPane.ERROR_MESSAGE);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public void addPartialToReturnsTable(){
        try{
            String returnID = defectiveItemsTableManager.getIDFromTable(defectiveItemsTableManager.getSelectedRow());
            String productName = defectiveItemsTableManager.getValueAt(defectiveItemsTableManager.getSelectedRow(), 2);
            String totalQuantity = defectiveItemsTableManager.getValueAt(defectiveItemsTableManager.getSelectedRow(), 5);
            String remarks = defectiveItemsTableManager.getValueAt(defectiveItemsTableManager.getSelectedRow(), 6);
            
            String quantity = inputQuantityInput.getText();
            if(Integer.parseInt(quantity) <= Integer.parseInt(totalQuantity) && Integer.parseInt(quantity) > 0){
                returnsIDs.add(returnID);
                
                returnsTableManager.addRowContent(new String[]{returnID, productName, "", quantity, remarks, totalQuantity});
                updateReturnsTable();
                inputQuantityDialog.dispose();
                inputQuantityInput.setText("");
            }else if(Integer.parseInt(quantity) > Integer.parseInt(totalQuantity)){
                JOptionPane.showMessageDialog(null, "The quantity you entered exceeds the total quantity in store.", "Invalid quantity", JOptionPane.ERROR_MESSAGE);
            }else if(Integer.parseInt(quantity) == 0){
                JOptionPane.showMessageDialog(null, "The quantity entered cannot be zero. Please input a valid quantity.", "Invalid quantity", JOptionPane.ERROR_MESSAGE);
            }else if(Integer.parseInt(quantity) < 0){
                JOptionPane.showMessageDialog(null, "The quantity entered cannot be negative. Please input a valid quantity.", "Invalid quantity", JOptionPane.ERROR_MESSAGE);
            }
            
        }catch(NumberFormatException nfe){
            JOptionPane.showMessageDialog(null, "Please input a valid quantity.", "Invalid quantity", JOptionPane.ERROR_MESSAGE);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void updateReturnedTable(){
        returnedTableManager.clearTableContents();
        try{
            Object supplierComboBoxitem = returnedSupplierComboBox.getSelectedItem();
                ComboItem ci = ((ComboItem)supplierComboBoxitem);
                String supplierID = ci.getValue();
            ResultSet rs = dbConnector.query("SELECT return_id, return_date, name, supplier_name, (return_quantity - replaced_quantity) AS quantity FROM returns JOIN product USING(product_id) JOIN supplier ON supplier.supplier_id = returns.supplier_id WHERE item_status = 'returned' AND returns.supplier_id = ? AND return_quantity - replaced_quantity != 0", new String[]{supplierID});
            while(rs.next()){
                String returnID = rs.getString(1);
                String dateReturned = rs.getString(2);
                String productName = rs.getString(3);
                String supplierName = rs.getString(4);
                String returnQuantity = rs.getString(5);
                
                Date date = dfto.parse(dateReturned);
                String retDate = sdf.format(date);
                
            
                if(returnedIDs.contains(returnID)){
                        for(int i = 0; i<replacedTableManager.getRowCount(); i++){
                            if(returnID.equals(replacedTableManager.getIDFromTable(i)) && Integer.parseInt(returnQuantity) > Integer.parseInt(replacedTableManager.getValueAt(i, 3))){
                                int partialQuantity = Integer.parseInt(returnQuantity) - Integer.parseInt(replacedTableManager.getValueAt(i, 3));
                                //System.out.print("Partial:" + partialQuantity);
                                
                                returnedTableManager.addRowContent(new String[]{returnID, retDate, productName, supplierName, Integer.toString(partialQuantity)});
                            }else if(returnID.equals(replacedTableManager.getIDFromTable(i)) && Integer.parseInt(returnQuantity) == Integer.parseInt(replacedTableManager.getValueAt(i, 3))){
                                
                            }
                        }

                    }else{
                        returnedTableManager.addRowContent(new String[]{returnID, retDate, productName, supplierName, returnQuantity});
                    }
            
            }
        }catch(SQLException slqE){
            returnedSupplierComboBox.addItem(new ComboItem(null, "None"));
        }catch(ParseException pe){
            pe.printStackTrace();
        }catch(NullPointerException npe){
            return;
        }
    }
    
    public void addToReplacedTable(){
        try{
            String returnID = returnedTableManager.getIDFromTable(returnedTableManager.getSelectedRow());
            String productName = returnedTableManager.getValueAt(returnedTableManager.getSelectedRow(), 2);
            String supplierName = returnedTableManager.getValueAt(returnedTableManager.getSelectedRow(), 3);
            String totalQuantity = returnedTableManager.getValueAt(returnedTableManager.getSelectedRow(), 4);
            String quantity = replacementQuantityInput.getText();
            returnedIDs.add(returnID);
            if(Integer.parseInt(quantity) <= Integer.parseInt(totalQuantity) && Integer.parseInt(quantity) > 0){
            replacedTableManager.addRowContent(new String[]{returnID, productName, supplierName, quantity, totalQuantity});
            replacementInputQuantityDialog.dispose();
            updateReturnedTable();
            }else if(Integer.parseInt(quantity) > Integer.parseInt(totalQuantity)){
                JOptionPane.showMessageDialog(null, "The quantity you entered exceeds the total quantity.", "Invalid quantity", JOptionPane.ERROR_MESSAGE);
            }else if(Integer.parseInt(quantity) == 0){
                JOptionPane.showMessageDialog(null, "The quantity entered cannot be zero. Please input a valid quantity.", "Invalid quantity", JOptionPane.ERROR_MESSAGE);
            }else if(Integer.parseInt(quantity) < 0){
                JOptionPane.showMessageDialog(null, "The quantity entered cannot be negative. Please input a valid quantity.", "Invalid quantity", JOptionPane.ERROR_MESSAGE);
            }
        }catch(NullPointerException npe){
            JOptionPane.showMessageDialog(null, "Please input a quantity.", "No quantity input detected", JOptionPane.ERROR_MESSAGE);
        }
        
    }
    public void changeSupplier(){
        try{
            if(supplierComboBox.getSelectedItem().toString().equals("All")){
                supplierName.setText("");
                updateReturnsTable();
                addButton.setEnabled(false);
                 info.setVisible(true);
            }else{
                info.setVisible(false);
                if(returnsTableManager.getRowCount()>0){
                    int response = JOptionPane.showConfirmDialog(null,"You have items in the list with the current supplier. \nChanging the supplier will clear the list. \n\nContinue?", "Change supplier", JOptionPane.YES_NO_OPTION);
                    if(response == 0){
                        returnsTableManager.clearTableContents();
                        addButton.setEnabled(true);
                        updateReturnsTable();
                    }else{
                        for(int i = 0; i < supplierComboBox.getItemCount(); i++){
                            if( supplierComboBox.getItemAt(i).getLabel().equals(supplierName.getText()) ){
                                supplierComboBox.setSelectedItem(supplierComboBox.getItemAt(i));
                            }
                        }
                        addButton.setEnabled(true);
                    }
                }else{
                    updateReturnsTable();
                    addButton.setEnabled(true);
                    supplierName.setText(supplierComboBox.getSelectedItem().toString());
                }
            }
        }catch(NullPointerException npe){
           
        }
    }
    public void changeSupplierReturnedTable(){
        try{
            if(replacedTableManager.getRowCount()>0){
                    int response = JOptionPane.showConfirmDialog(null,"You have items in the list with the current supplier. \nChanging the supplier will clear the list. \n\nContinue?", "Change supplier", JOptionPane.YES_NO_OPTION);
                    if(response == 0){
                        replacedTableManager.clearTableContents();
                        updateReturnedTable();
                        returnedIDs.clear();
                        replacedSupplierText.setText(returnedSupplierComboBox.getSelectedItem().toString());
                    }else{
                        for(int i = 0; i < returnedSupplierComboBox.getItemCount(); i++){
                            if( returnedSupplierComboBox.getItemAt(i).getLabel().equals(replacedSupplierText.getText()) ){
                                returnedSupplierComboBox.setSelectedItem(returnedSupplierComboBox.getItemAt(i));
                            }
                        }
                    }
                }
        else{
            replacedSupplierText.setText(returnedSupplierComboBox.getSelectedItem().toString());
            updateReturnedTable();
        }
        }catch(NullPointerException npe){
            return;
        }
    }
    public void checkIfMultiple(){
        try{
            int quantity = Integer.parseInt(returnedTableManager.getValueAt(returnedTableManager.getSelectedRow(), 4));
            System.out.println("Quantity:" + quantity);
            if(quantity > 1){
                
                totalQuantityLabel.setText("The product has a total quantity of " + quantity + ".");
                replacementQuantityInput.setText(returnedTableManager.getValueAt(returnedTableManager.getSelectedRow(), 4));
                replacementInputQuantityDialog.setVisible(true);
            }else if(quantity == 1){
                String returnID = returnedTableManager.getIDFromTable(returnedTableManager.getSelectedRow());
                String productName = returnedTableManager.getValueAt(returnedTableManager.getSelectedRow(), 2);
                String supplierName = returnedTableManager.getValueAt(returnedTableManager.getSelectedRow(), 3);
                
                returnedIDs.add(returnID);
                replacedTableManager.addRowContent( new String[]{returnID, productName, supplierName, Integer.toString(quantity), Integer.toString(quantity)});
                updateReturnedTable();
            }
        }catch(ArrayIndexOutOfBoundsException aioobe){
            aioobe.printStackTrace();
            JOptionPane.showMessageDialog(null, "Please select an item from the table", "No product selected", JOptionPane.ERROR_MESSAGE);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void returnToSupplier(){
        try{
            String dateInput = dateReturnedDateChooser.getText().split(",")[1].substring(1).concat(","+ dateReturnedDateChooser.getText().split(",")[2]);
            DateFormat dffrom = new SimpleDateFormat("MMMMM d, yyyy");
            SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
            
            Date date = dffrom.parse(dateInput);
            String warrantyReturnDate = sdformat.format(date);
            String remarksText = remarksInput.getText();
            int response = JOptionPane.showConfirmDialog(null, "Confirm Returns?", "Confirm transaction", JOptionPane.OK_CANCEL_OPTION);
            if(response == 0){
                int count = 0;
                for(int i = 0; i< returnsTableManager.getRowCount(); i++){
                    String retID = returnsTableManager.getIDFromTable(i);
                    if(returnsTableManager.getValueAt(i,3).equals(returnsTableManager.getValueAt(i,5))){
                        dbConnector.update("UPDATE returns SET return_date = ?, item_status = 'returned', remarks = ? WHERE return_id = ?", new String[]{warrantyReturnDate, remarksText, retID});
                    }
                    else{
                        dbConnector.update("UPDATE returns SET return_quantity = return_quantity - ? WHERE return_id = ?", new String[]{returnsTableManager.getValueAt(i,3), retID});
                        ResultSet rs = dbConnector.query("SELECT supplier_id, product_id, remarks, replacement_id FROM returns WHERE return_id = ?", retID);
                        rs.next();
                        String supplierID = rs.getString(1);
                        String productID = rs.getString(2);
                        String remarks = rs.getString(3);
                        String replacementID = rs.getString(4);
                        
                        dbConnector.insert("INSERT INTO returns (supplier_id, product_id, return_date, remarks, item_status, replacement_id, return_quantity) VALUES(?,?,?,?,?,?,?)",
                                new String[]{supplierID, productID, warrantyReturnDate, remarks, "returned", replacementID, returnsTableManager.getValueAt(i,3)});
                    }
                    count++;
                }
                updateReturnsTable();
                filterReturnedSupplierComboBox();
                updateReturnedTable();
                remarksInput.setText("");
                returnsTableManager.clearTableContents();
                JOptionPane.showMessageDialog(null,count + " product(s) was/were returned to " + supplierName.getText() + ".", "Transaction successful" ,JOptionPane.INFORMATION_MESSAGE);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public void markAsReplacedBySupplier(){
        try{
            String dateInput = dateReplacedDateChooser.getText().split(",")[1].substring(1).concat(","+ dateReplacedDateChooser.getText().split(",")[2]);
            DateFormat dffrom = new SimpleDateFormat("MMMMM d, yyyy");
            SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
            
            Date date = dffrom.parse(dateInput);
            String warrantyReturnDate = sdformat.format(date);
            int response = JOptionPane.showConfirmDialog(null, "Confirm Returns?", "Confirm transaction", JOptionPane.OK_CANCEL_OPTION);
            if(response == 0){
                int count = 0;
                for(int x = 0; x < replacedTableManager.getRowCount(); x++){
                    String returnID = replacedTableManager.getIDFromTable(x);
                    String quantity = replacedTableManager.getValueAt(x,3);
                    String remarks = replacedRemarksInput.getText();
                    dbConnector.update("UPDATE returns SET replaced_quantity = replaced_quantity + ? WHERE return_id = ?", new String[]{quantity, returnID});
                    dbConnector.insert("INSERT INTO returned(return_id, replaced_quantity, remarks, dateReplaced) VALUES(?,?,?,?)", new String[]{returnID, quantity, remarks, warrantyReturnDate});
                    count++;
                }
                returnedIDs.clear();
                replacedTableManager.clearTableContents();
                replacedRemarksInput.setText("");
                filterReturnedSupplierComboBox();
                updateReturnedTable();
                
                JOptionPane.showMessageDialog(null, count + " product(s) was/were replaced by the supplier.", "Transaction Successful", JOptionPane.INFORMATION_MESSAGE);
            }
        }catch(SQLException sqlE){
            sqlE.printStackTrace();
        }catch(ParseException pe){
            
        }
    }
    public void filterReturnsSupplierComboBox(){
        try{
            supplierComboBox.removeAllItems();
            ResultSet rs = dbConnector.query("SELECT DISTINCT(returns.supplier_id), supplier_name FROM returns JOIN Supplier USING(supplier_id) WHERE item_status = 'in store'");
            while(rs.next()){
                String supplierID = rs.getString(1);
                String supplierName = rs.getString(2);

                supplierComboBox.addItem(new ComboItem(supplierID, supplierName));
            }
            supplierComboBox.setSelectedIndex(0);
            supplierName.setText(supplierComboBox.getSelectedItem().toString());
        }catch(SQLException sqlE){
            sqlE.printStackTrace();
        }catch(IllegalArgumentException iae){
            supplierName.setText("");
        }
    }
    public void filterReturnedSupplierComboBox(){
        try{
            returnedSupplierComboBox.removeAllItems();
            ResultSet rs = dbConnector.query("SELECT DISTINCT(returns.supplier_id), supplier_name FROM returns JOIN Supplier USING(supplier_id) WHERE return_quantity != replaced_quantity AND item_status = 'returned'");
            while(rs.next()){
                String supplierID = rs.getString(1);
                String supplierName = rs.getString(2);

                returnedSupplierComboBox.addItem(new ComboItem(supplierID, supplierName));
            }
            returnedSupplierComboBox.setSelectedIndex(0);
            replacedSupplierText.setText(returnedSupplierComboBox.getSelectedItem().toString());
        }catch(SQLException sqlE){
            sqlE.printStackTrace();
        }catch(IllegalArgumentException iae){
            replacedSupplierText.setText("");
        }
    }

    public void justDClickIt(JTable table){
    table.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent me) {
        JTable table =(JTable) me.getSource();
        Point p = me.getPoint();
        int row = table.rowAtPoint(p);
        if (me.getClickCount() == 2 && !supplierName.getText().equals("")) {
            add.doClick();
        }
    }
});
    
    }
    public void justDClickIt2(JTable table){
    table.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent me) {
        JTable table =(JTable) me.getSource();
        Point p = me.getPoint();
        int row = table.rowAtPoint(p);
        if (me.getClickCount() == 2) {
            checkIfMultiple();
        }
    }
});
    }
    
    public void updateReturnsHistory(){
        returnsHistoryTableManager.clearTableContents();
        DateFormat dfto = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("MMMMM d, yyyy");
        String toDate = InputValidator.dateFormat(dateFrom);
        String fromDate = InputValidator.dateFormat(dateTo);
        try{
            ResultSet rs = dbConnector.query("SELECT return_id, return_date, return_quantity, name, supplier_name, remarks, serial_number FROM returns JOIN product using(Product_id) JOIN supplier ON supplier.supplier_id = returns.supplier_id WHERE return_date between '" + toDate + "' and '" + fromDate + "' ORDER BY 2 DESC");

            while(rs.next()){
                String date = rs.getString("return_date");
                Date dateFormat = dfto.parse(date);
                String dateReport = sdf.format(dateFormat);
                String name = rs.getString("name");
                String quantity = rs.getString("return_quantity");
                String description = rs.getString("supplier_name");
                String remarks = rs.getString("remarks");
                String value2 = rs.getString("serial_number");
                String value1 = rs.getString("return_id");
                String[] reportDetails = {dateReport, name, value2, quantity, remarks, value1};
                returnsHistoryTableManager.addRowContent(reportDetails);
            }
        }catch(SQLException sqlE){
            sqlE.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }                
    }
    
    public void clearList(){
        int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to clear the list?", "Confirm Checkout", JOptionPane.YES_NO_OPTION);
        if(result == 0){
            returnsTableManager.clearTableContents();
        }
    }
    
    public void removeSelectedRow(){        
        returnsIDs.remove(returnsTableManager.getIDFromTable(returnsTableManager.getSelectedRow()));
        returnsTableManager.deleteRow();
        updateReturnsTable();
    }
    
    public void editQuantity(){
        String qty = editQuantityInput.getText();
        returnsTableManager.setValueAt(returnsTableManager.getSelectedRow(), 3, qty);
    }
}
